package ua.com.infinite.mobile.adapters;

import java.util.List;

import ua.com.infinite.mobile.R;
import ua.com.infinite.mobile.plains.NewsItem;
import ua.com.infinite.mobile.utils.imageloader.ImageLoader;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AdapterNews extends ArrayAdapter<NewsItem> {

	private Context context;
	private int layoutRes;
	private List<NewsItem> items;
	private ImageLoader imageLoader;

	public AdapterNews(Context context, int viewResourceId, List<NewsItem> objects) {
		super(context, viewResourceId, objects);
		this.context = context;
		this.layoutRes = viewResourceId;
		this.items = objects;
		this.imageLoader = new ImageLoader(context);

	}

	@Override
	public View getView(int pos, View convertView, ViewGroup parent) {
		View row = convertView;
		NewsItemHolder holder = null;

		if (row == null) {
			row = LayoutInflater.from(context).inflate(R.layout.news_row, null);

			holder = new NewsItemHolder();
			holder.poster_icon = (ImageView) row.findViewById(R.id.poster_icon);
			holder.poster_name = (TextView) row.findViewById(R.id.poster_name);
			holder.post_title = (TextView) row.findViewById(R.id.post_title);
			holder.post_text = (TextView) row.findViewById(R.id.post_text);
			holder.post_img = (ImageView) row.findViewById(R.id.post_img);
			holder.post_date = (TextView)row.findViewById(R.id.post_date);

			row.setTag(holder);

		} else {
			holder = (NewsItemHolder) row.getTag();
		}

		NewsItem item = items.get(pos);
		imageLoader.DisplayImage(item.poster_icon, holder.poster_icon);
		holder.poster_name.setText(item.poster_name);
		holder.post_title.setText(item.post_title);
		holder.post_text.setText(item.post_text);
		if (item.post_imgs!=null && item.post_imgs.length > 0) {
			holder.post_img.setVisibility(ImageView.VISIBLE);
			imageLoader.DisplayImage(item.post_imgs[0], holder.post_img);
		}else{
			holder.post_img.setVisibility(ImageView.GONE);
		}
		holder.post_date.setText(item.post_date);

		return row;
	}

	static class NewsItemHolder {
		ImageView poster_icon;
		TextView poster_name;
		TextView post_title;
		TextView post_text;
		ImageView post_img;
		TextView post_date;
	}

}
